﻿using System;
using System.Diagnostics;
using System.Linq;

namespace Susam
{
    abstract class SusamJunctionBase
    {
        protected IQueryable<System.Data.DataRow> table;
        protected delegate System.Data.DataRow AddToTable(int id1, int id2);
        protected AddToTable addToTable;

        protected virtual bool MatchExists(int id1, int id2)
        {
            bool ret = false;
            var MatchList =
                from row in table
                where ((int)row[0] == id1 && (int)row[1] == id2)
                select row;

            if (MatchList.Any())
                ret = true;

            return ret;
        }

        public void AddMatch(int id1, int id2)
        {
            // TODO:
            // Use Insert or Update?
            if (!MatchExists(id1, id2))
            {
                Debug.WriteLine("Creating new junction table entry...");
                var row = addToTable(id1, id2);
                SusamDS.DataSet.AcceptChanges();
            }
        }
    }

    class BookAuthorJunction : SusamJunctionBase
    {
        public BookAuthorJunction()
        {
            table = (IQueryable<System.Data.DataRow>)SusamDS.DataSet.jncBookAuthor.AsQueryable<System.Data.DataRow>();
            addToTable = SusamDS.DataSet.jncBookAuthor.AddjncBookAuthorRow;
        }
    }

    class BookPublisherJunction : SusamJunctionBase
    {
        public BookPublisherJunction()
        {
            table = (IQueryable<System.Data.DataRow>)SusamDS.DataSet.jncBookAuthor.AsQueryable<System.Data.DataRow>();
            addToTable = SusamDS.DataSet.jncBookPublisher.AddjncBookPublisherRow;
        }
    }

    class BookLanguageJunction : SusamJunctionBase
    {
        public BookLanguageJunction()
        {
            table = (IQueryable<System.Data.DataRow>)SusamDS.DataSet.jncBookLanguage.AsQueryable<System.Data.DataRow>();
            addToTable = SusamDS.DataSet.jncBookLanguage.AddjncBookLanguageRow;
        }
    }

    class BookFileTypeJunction : SusamJunctionBase
    {
        public BookFileTypeJunction()
        {
            table = (IQueryable<System.Data.DataRow>)SusamDS.DataSet.jncBookFileType.AsQueryable<System.Data.DataRow>();
            addToTable = SusamDS.DataSet.jncBookFileType.AddjncBookFileTypeRow;
        }
    }

    class BookContributorJunction
    {
        protected IQueryable<System.Data.DataRow> table;
        
        public BookContributorJunction()
        {
            table = (IQueryable<System.Data.DataRow>)SusamDS.DataSet.jncBookContributor.AsQueryable<System.Data.DataRow>();
        }

        protected virtual bool MatchExists(int id1, int id2, String role)
        {
            bool ret = false;
            var MatchList =
                from row in table
                where ((int)row[0] == id1 && (int)row[1] == id2 && role.Equals((String)row[2]))
                select row;

            if (MatchList.Any())
                ret = true;

            return ret;
        }

        public void AddMatch(int id1, int id2, String role)
        {
            if (!MatchExists(id1, id2, role))
            {
                Debug.WriteLine("Creating new junction table entry...");
                var row = SusamDS.DataSet.jncBookContributor.AddjncBookContributorRow(id1, id2, role);
                SusamDS.DataSet.AcceptChanges();
            }
        }
    }
}
